<?php

namespace app\manage\controller;

use app\common\controller\Manage;
use app\common\model\Rechange as RechangeModel;

class Rechange extends Manage
{
    public $RechangeModel = null;
    
    public function initialize() 
    {
        parent::initialize();
        $this->RechangeModel = new RechangeModel(); 
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if($this->request->isAjax()) {
            $condition = [];
            if($this->request->post('status')) {
                $condition[] = ['rechange.status', 'eq',$this->request->post('status')];
            }
            if($this->request->post('handle') === '0') {
                $condition[] = ['rechange.handle', 'eq',$this->request->post('handle')];
            }
            if($this->request->post('mobile')) {
                $condition[] = ['user.mobile', 'like', '%'+$this->request->post('mobile')+'%'];
            }
            $page = $this->request->post('page', 1);
            $limit = $this->request->post('limit', 15);
            $list = $this->RechangeModel->alias('rechange')->join('user', 'user.id = rechange.user_id')->field('rechange.*, user.id as user_id, user.mobile, user.nickname, user.balance')->where($condition)->limit(($page-1)*$limit, $limit)->order('rechange.id', 'desc')->select();
            $count = $this->RechangeModel->alias('rechange')->join('user', 'user.id = rechange.user_id')->where($condition)->count();
            foreach ($list as &$info) {
                $info['ctime2'] = date('Y-m-d H:i:s', $info['ctime']);
            }
            return [
                'code' => 0,
                'count' => $count,
                'data' => $list
            ];
        }
        return $this->fetch();
    }

    public function get(){
        $result = [
            'status' => true,
            'msg'    => '',
            'data'   => '',
        ];
        $condition = [];
        $condition[] = ['rechange.id', 'eq',$this->request->post('id')];
        $result['data'] = $this->RechangeModel->alias('rechange')->join('user', 'user.id = rechange.user_id')->field('rechange.*, user.id as user_id, user.mobile, user.nickname,  user.balance')->where($condition)->find();
        $result['data']['ctime2'] = date('Y-m-d H:i:s', $result['data']['ctime']);
        return $result;
    }
    
    public function handle()
    {
        return $this->RechangeModel->where('id', $this->request->post('id'))->setField('handle', '1');
    }
}
